package com.lixin.smartclassroom.ui.home.tab2.fragment

import android.view.LayoutInflater
import android.view.ViewGroup
import com.drake.brv.utils.bindingAdapter
import com.drake.channel.receiveEvent
import com.drake.net.utils.scopeNetLife
import com.lixin.smartclassroom.R
import com.lixin.smartclassroom.api.mychildzhktlist
import com.lixin.smartclassroom.api.zhktlist
import com.lixin.smartclassroom.bean.DataListBean
import com.lixin.smartclassroom.common.sourceSetDialog
import com.lixin.smartclassroom.databinding.FragmentRecyclerCustomBinding
import com.lixin.smartclassroom.ext.child_id
import com.lixin.smartclassroom.ext.user_role
import com.lixin.smartclassroom.ui.home.adapter.courseMyAttend
import com.lixin.smartclassroom.ui.home.adapter.courseMyCreate
import com.lixin.smartclassroom.ui.home.adapter.courseRecommend
import com.lixin.smartclassroom.ui.home.adapter.messageList
import com.lixin.smartclassroom.ui.home.tab2.activity.SmartCourseDetailAct
import com.lixin.smartclassroom.ui.home.tab5.activity.EventCommentAct
import com.lixinkeji.base.BaseFragment
import com.lixinkeji.extension.getIntArgument
import com.lixinkeji.extension.gone
import com.lixinkeji.extension.start
import com.lixinkeji.extension.visible

/**
 * Description
 * Author: lwc
 * Date:2023/12/6 14:03
 */
class Tab2ChildFragment : BaseFragment<FragmentRecyclerCustomBinding>() {
    override fun getViewBinding(
        inflater: LayoutInflater, container: ViewGroup?
    ) = FragmentRecyclerCustomBinding.inflate(inflater)

    private lateinit var listBean: ArrayList<DataListBean>
    private var type = 0
    override fun iniView() {
        type = getIntArgument("type")

        receiveEvent<String>("refreshClassList") {
            binding.refresh.refresh()
        }

        if (type == 1) binding.recycler.courseMyCreate().apply {
            R.id.btnSet.onClick {
                requireContext().sourceSetDialog(
                    "1",
                    models?.get(modelPosition) as DataListBean
//                    (models?.get(modelPosition) as DataListBean).id!!,
//                    (models?.get(modelPosition) as DataListBean).quanxian!!,
//                    (models?.get(modelPosition) as DataListBean).customerids!!,
//                    (models?.get(modelPosition) as DataListBean).state!!
                )
            }
        }
        else binding.recycler.courseMyAttend(1).apply {
            R.id.rl.onClick {
                start(SmartCourseDetailAct::class.java) {
                    putExtra("type", "1")
                    putExtra("courseid", (models?.get(modelPosition) as DataListBean).id)
                }
            }
        }


        binding.apply {
            refresh.onRefresh {
                scopeNetLife {
                    listBean = when (requireContext().user_role) {
                        1 -> {
                            if (requireContext().child_id.isEmpty()){
                                 ArrayList()
                            }else{
                                mychildzhktlist(index, 10, requireContext().child_id)!!
                            }
                        }

                        else -> zhktlist(index, 10, type.toString())!!
                    }


                    addData(listBean) {
                        if (binding.recycler.bindingAdapter.models?.size == 0) {
                            binding.state.showEmpty()
                            binding.recycler.gone()
                        } else {
                            binding.recycler.visible()
                        }
                        listBean.size == 10
                    }
                }
            }.autoRefresh()
        }
    }

    override fun onResume() {
        super.onResume()
        binding.refresh.autoRefresh()
    }
}